[求助]有没有C语言高手啊,关于execv的用法

来源:百度知道 编辑:UC知道 时间:2024/06/17 06:17:50
最近刚接触C,需要做一个minishell
类似linux下的bash,不过要简单很多很多
用fork()和execv()实现
怎么用execv()来运行输入的指令呢。。。
比system()要复杂的多啊 @_@

测试不带参数的
execv(str, NULL);
str是输入的路径
貌似没有反应...
man execv看不太懂
带参数的又应该要怎么写?

第一次用百度知道,不知道发的对不对
哪位能指导一下啊。。。大感谢

  execv用法介绍:

  execv会停止执行当前的进程,并且以progname应用进程替换被停止执行的进程,进程ID没有改变。

  如果应用程序正常执行完毕,那么execv是永远不会返回的;当execv在调用进程中返回时,说明这个应用程序应该出错了,此时它的返回值应该是-1,具体的错误代码可以通过全局变量errno查看,还可以通过stderr得到具体的错误描述字符串。

  示例如下:

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>


main(void)
{
  pid_t pid = fork();
   
  if( pid == 0 ) // this is the child process
  {
     execv("/bin/ls", NULL);

     // the program should not reach here, or it means error occurs during execute the ls command.
     printf("command ls is not f